package com.example.cardgame

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import androidx.gridlayout.widget.GridLayout
import com.example.cardgame.model.CardMatchingGame

class ThirdActivity : AppCompatActivity(),View.OnClickListener{
    lateinit var game:CardMatchingGame
    val cardButtons = mutableListOf<Button>()
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_third)
        //        val intent = getIntent()
//        val textView: TextView = this.findViewById(R.id.textView)
//        textView.text = intent.getStringExtra(SCORE)
//
        fun onBackPressed() {
            // 当用户按下返回键时，设置分数并结束活动
            val intent = Intent()
            intent.putExtra(SCORE, game.score.toString())
            setResult(Activity.RESULT_OK, intent)
            finish()
            super.onBackPressed()
        }

        val back = findViewById<android.widget.Button>(R.id.button_back)
        back.setOnClickListener{
            val intent = Intent()
            intent.putExtra(SCORE, game.score.toString())
            setResult(Activity.RESULT_OK, intent)
            finish()
//            val intent = Intent(this, MainActivity::class.java)
//            startActivity(intent)
        }
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//
//        }
        val gridLayout = findViewById<GridLayout>(R.id.gridLayout)
        for (v in gridLayout.children) {
            if (v is Button) {
                v.setOnClickListener(this)
                cardButtons.add(v)
            }
        }
        game = CardMatchingGame(cardButtons.count())
        updateUI()

        val reset = findViewById<Button>(R.id.button19)
        reset.setOnClickListener {
            game = CardMatchingGame(cardButtons.count())
            updateUI()
        }

    }
    fun updateUI() {
        for (button in cardButtons) {
            var index = cardButtons.indexOf(button)
            val card = game.cardAtIndex(index)
            button.isEnabled = !card.isMatched
            if (card.isChosen) {
                button.text = card.toString()
                button.setBackgroundColor(Color.WHITE)
            } else {
                button.text = ""
                button.setBackgroundResource(R.drawable.a)
            }
        }
        val score = findViewById<TextView>(R.id.textView)
        score.text = String.format("%s%d", "分数：", game.score)
    }
    override fun onClick(v: View?) {
        if (v is Button) {
            val index = cardButtons.indexOf(v)
            game.chooseCardAtIndex(index)
            updateUI()
        }
    }
}

private fun Button.setOnClickListener(thirdActivity: ThirdActivity) {

}
